<template>
    <div class="toggle-button-wrapper">
      <input type="checkbox" id="toggle-button" name="slider" :checked="checked" v-on="listeners" @change="onChange" />
      <label for="toggle-button" class="button-label">
        <div class="default">
          <span>30天</span>
          <span>今天</span>
        </div>
        <span class="circle">{{ checked ? onText : offText }}</span>
      </label>
    </div>
  </template>
  
  <script>
  export default {
    name: 'slider',
    inheritAttrs: false,
    props: {
      onText: {
        type: String,
        default: '今天'
      },
      offText: {
        type: String,
        default: '30天'
      },
      propsChecked: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        checked: true
      }
    },
    computed: {
      listeners() {
        let result = Object.assign({}, this.$listeners)
        return result
      }
    },
    methods: {
      onChange(e) {
        this.checked = e.target.checked
        this.$emit('change', e.target.checked)
      }
    },
    created() {
      this.checked = this.propsChecked
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .toggle-button-wrapper{
    float: right;
  }
  #toggle-button {
    display: none;
  }
  .default {
    display: flex;
    justify-content: space-around;
    align-items: center;
    font-size: 24px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #8c8ea5;
    line-height: 24px;
    height: 40px;
  }
  .button-label {
    position: relative;
    display: inline-block;
    box-sizing: border-box;
    width: 160px;
    height: 40px;
    border-radius: 26px;
    background: rgba(140, 142, 165, 0.1);
  }
  .circle {
    position: absolute;
    top: 0;
    line-height: 40px;
    text-align: center;
    transition: all 0.3s ease;
    font-size: 24px;
    color: #fff;
    width: 80px;
    height: 40px;
    background: #5867ff;
    border-radius: 26px;
  }
  .button-label .circle {
    left: -1px;
    right: auto;
    background: #5867ff;
    color: #ffffff;
  }
  #toggle-button:checked + label.button-label {
  }
  #toggle-button:checked + label.button-label .circle {
    left: 75px;
    right: -1px;
    background: #5d75f7;
  }
  </style>
  
  